﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Framework.Utility;
using System.ComponentModel;
using Sector7.Medical.Utility;
using Sector7.Framework.Enums;
using Sector7.Medical.Biz;
using Sector7.Medical.INFM.Windows;
using Sector7.Medical.Consts;
using Sector7.Medical.EntityType;

namespace Sector7.Medical.INFM.Controls
{
    /// <summary>
    /// UnitSummaryControl.xaml 的交互逻辑
    /// </summary>
    public partial class UnitSummaryControl : UserControl
    {
        #region Accessor
        #endregion

        #region Public functions
        #endregion

        #region Private functions
        private void GetData()
        {
            Senvironments.ProgressWindow.Visibility = Visibility.Visible;

            var worker = BackgroundWorkerUtil.GetWorker(this);
            worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
            worker.RunWorkerAsync();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            GetData();
        }

        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            m_unitList = Agent_INFS.GetUnitList();

            this.Dispatcher.BeginInvoke(new DelegateWithNothing(UpdateUnitList));
        }

        private void UpdateUnitList()
        {
            ClientUtil.LoadItemsSource(dtgMain, m_unitList);

            Senvironments.ProgressWindow.Visibility = Visibility.Hidden;
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            var addWindow = new AddUnitWindow();
            addWindow.InitializeWindow(CrudType.Create);
            addWindow.AddSuccess += new EventHandler(AddUnitWindow_AddSuccess);
            addWindow.ShowDialog();
        }

        private void AddUnitWindow_AddSuccess(object sender, EventArgs e)
        {
            var addWindow = sender as AddUnitWindow;
            var unit = addWindow.Unit;
            switch (addWindow.Crud)
            {
                case CrudType.Create:
                    m_unitList.Add(unit);
                    ClientUtil.LoadItemsSource(dtgMain, m_unitList);
                    dtgMain.SelectedItem = unit;
                    break;
                case CrudType.Update:
                    ClientUtil.UpdateToItems<M7_Unit>(m_unitList, C_ColumnName.UNIT_ID, unit);
                    dtgMain.Items.Refresh();
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region Structure
        public UnitSummaryControl()
        {
            InitializeComponent();
        }
        #endregion

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            var selectedItem = dtgMain.SelectedItem;
            if (selectedItem != null)
            {
                var modifyWindow = new AddUnitWindow();
                modifyWindow.Unit = selectedItem as M7_Unit;
                modifyWindow.InitializeWindow(CrudType.Update);
                modifyWindow.AddSuccess += new EventHandler(AddUnitWindow_AddSuccess);
                modifyWindow.ShowDialog();
            }
        }

        #region Inner members
        private List<M7_Unit> m_unitList = null;
        #endregion
    }
}
